package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.IOrderListService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: WD created on 2021-09-29 19:47
 * @Version:
 */
@Service
public class OrderListServiceImpl implements IOrderListService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderConverter orderConverter;
    @Autowired
    OrderShippingMapper orderShippingMapper;


    @Override
    public OrderListResponse queryOrderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        List<OrderDetailInfo> orderDetailInfoList = new ArrayList<>();
        Long userId = request.getUserId();


        try {
            request.requestCheck();

            PageHelper.startPage(request.getPage(), request.getSize(), request.getSort());
            Example example = new Example(Order.class);
            example.setOrderByClause("create_time desc");
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userId", userId).andNotEqualTo("status",8);
            List<Order> orders = orderMapper.selectByExample(example);
            for (Order order : orders) {
                String orderId = order.getOrderId();
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

                //查orderShipping
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                //查List<OrderItemDto>
                Example orderItemExample = new Example(OrderItem.class);
                orderItemExample.createCriteria().andEqualTo("orderId", orderId);
                List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                orderDetailInfoList.add(orderDetailInfo);
            }

            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
            response.setTotal(orderPageInfo.getTotal());
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setDetailInfoList(orderDetailInfoList);
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }


}
